package com.aditya.videoutil;

import java.io.File;
import java.util.logging.Logger;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.aditya.videoutil.util.FolderLayout;
import com.aditya.videoutil.util.IFolderItemListener;
import com.aditya.videoutil.util.VideoUtilConstants;
import com.aditya.videoutil.BrowseVideoActivity;
import com.aditya.videoutil.R;

public class BrowseVideoActivity extends VideoActivity implements IFolderItemListener {
  Logger logger = Logger.getLogger(BrowseVideoActivity.class.getName());

  FolderLayout localFolders;
  String requestType;
  String fileType;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getIntent().getStringExtra(VideoUtilConstants.FILE_TYPE) != null) {
      fileType = getIntent().getStringExtra(VideoUtilConstants.FILE_TYPE);
    }

    browseFile();
  }

  private void browseFile() {
    setContentView(R.layout.folders);

    localFolders = (FolderLayout) findViewById(R.id.localfolders);
    localFolders.setIFolderItemListener(this);

  }

  // Your stuff here for Cannot open Folder
  public void OnCannotFileRead(File file) {
    showCannotReadFileDialog(file);
  }

  /**
   * Called on file click
   */
  public void OnFileClicked(File file) {

    if (fileType == null && !file.getName().toLowerCase().endsWith(".mp4")) {
      getAlertDialog().setTitle("Invalid selection").setMessage("You can only select a .mp4 file for this request!")
          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
            }

          }).show();
    }  else {
      Intent result = new Intent();
      result.putExtra(VideoUtilConstants.FILE_URI, file.getAbsolutePath());
      setResult(Activity.RESULT_OK, result);
      finish();
    }

  }

  /**
   * On back press, take to previous folder
   */
  @Override
  public void onBackPressed() {
    File currDir = localFolders.getCurrDir();
    if (currDir != null && currDir.getParent() != null) {
      localFolders.setDir(currDir.getParent());
    } else {
      finish();
    }
  }
}
